package com.songlanyun.modules.areafeature.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.enums.GoodsConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.BeanUtil;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;
import com.songlanyun.common.utils.custom.ClassConvertUtils;
import com.songlanyun.modules.areafeature.dao.GoodsAreaFeatureDao;
import com.songlanyun.modules.areafeature.entity.GoodsAreaFeatureEntity;
import com.songlanyun.modules.areafeature.model.GoodsAreaFeatureDTO;
import com.songlanyun.modules.areafeature.model.GoodsAreaFeaturePageReq;
import com.songlanyun.modules.areafeature.model.GoodsAreaFeatureSlimVo;
import com.songlanyun.modules.areafeature.service.GoodsAreaFeatureService;
import com.songlanyun.modules.goods.entity.Goods;
import com.songlanyun.modules.goods.service.GoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("goodsAreaFeatureService")
public class GoodsAreaFeatureServiceImpl extends ServiceImpl<GoodsAreaFeatureDao, GoodsAreaFeatureEntity> implements GoodsAreaFeatureService {


    @Autowired
    private GoodsService goodsService;

    @Override
    public GoodsAreaFeatureEntity create(GoodsAreaFeatureEntity entity) {
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public GoodsAreaFeatureEntity modify(GoodsAreaFeatureEntity entity) {
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public void remove(Long id) {
        this.removeById(id);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsAreaFeatureSlimVo> page = this.baseMapper.queryAreaFeaturePage(new Query<GoodsAreaFeatureSlimVo>().getPage(params), paramsMapToReq(params));
        return new PageUtils(page);
    }

    @Override
    public List<GoodsAreaFeatureSlimVo> queryList(Map<String, Object> params) {
        return this.baseMapper.queryList(paramsMapToReq(params));

    }

    @Override
    public PageUtils welfarePage(Map<String, Object> params) {
        IPage<GoodsAreaFeatureSlimVo> page = this.baseMapper.queryWelfarePagePage(new Query<GoodsAreaFeatureSlimVo>().getPage(params), paramsMapToReq(params));
        return new PageUtils(page);
    }

    @Override
    public GoodsAreaFeatureEntity loadByGoodsId(Long goodsId) {
        return this.baseMapper.selectOne(new LambdaQueryWrapper<GoodsAreaFeatureEntity>().eq(GoodsAreaFeatureEntity::getGoodsId,goodsId).last("limit 1"));
    }

    private GoodsAreaFeaturePageReq paramsMapToReq(Map<String, Object> params)
    {
        GoodsAreaFeaturePageReq req = new GoodsAreaFeaturePageReq();
        Integer areaFeature = MapUtil.getInt(params, "area_feature");
        req.setAreaFeature(areaFeature);
        String title = MapUtil.getStr(params, "title");
        Long categoryId = MapUtil.getLong(params, "category_id");
        //区分复购区产品
        if(categoryId == (long)GoodsConstant.AreaFeature.EXCHANGING.getCode())
        {
            req.setCategoryId((long)GoodsConstant.AreaFeature.EXCHANGING.getCode());
        }
        // 兑换区产品
        if(categoryId == 144L)
        {
            req.setCategoryId(144L);
        }
        if(StringUtils.isNotEmpty(title))
        {
            req.setTitle(title);
        }

        Integer added = MapUtil.getInt(params, "added");
        if(ObjectUtil.isNotNull(added) && added>-1)
        {
            req.setAdded(added);
        }

        if (params.get("order_type") != null) {
            req.setOrderType(ClassConvertUtils.objToInteger(params.get("order_type")));
        }

        if (params.get("order_mode") != null) {
            req.setOrderMode(params.get("order_mode").toString());
        }
        Long hxCategoryId = MapUtil.getLong(params, "hx_category_id");
        if (hxCategoryId != null) {
            req.setHxCategoryId(hxCategoryId);
        }


        Integer limit = MapUtil.getInt(params, "limit");
        if(ObjectUtil.isNotNull(limit) && limit>0)
        {
            req.setLimit(limit);
        }
        return req;
    }

    @Override
    public void add(GoodsAreaFeatureDTO dto) {
        Goods goods = goodsService.getById(dto.getGoodsId(), true);
        if(!goods.isVerify())
        {
            throw new RRException("商品没有通过审核");
        }

        GoodsAreaFeatureEntity load = loadByGoodsId(dto);
        if(ObjectUtil.isNotNull(load))
        {
            throw new RRException("商品已推送到了"+load.getAreaFeature().getMsg()+"分区");
        }
        GoodsAreaFeatureEntity goodsAreaFeatureEntity = new GoodsAreaFeatureEntity();
        BeanUtil.copyPropertiesIgnoreNull(dto,goodsAreaFeatureEntity);
        goodsAreaFeatureEntity.setAreaFeature(GoodsConstant.AreaFeature.getByCode(dto.getAreaFeature()));
        goodsAreaFeatureEntity.setServicePackageArea(null);


        goodsAreaFeatureEntity.setCreateTime(new Date());
        this.create(goodsAreaFeatureEntity);
    }

    @Override
    public void updateOrderIndex(GoodsAreaFeatureDTO dto) {
        GoodsAreaFeatureEntity obj = this.getById(dto.getId());
        if(ObjectUtil.isNull(obj))
        {
            throw new RRException("没有找到编号为"+dto.getId()+"商品分区数据");
        }
        obj.setOrderIndex(dto.getOrderIndex());
        this.modify(obj);
    }



    private GoodsAreaFeatureEntity loadByGoodsId(GoodsAreaFeatureDTO dto) {
        GoodsAreaFeatureEntity obj = this.baseMapper.selectOne(new LambdaQueryWrapper<GoodsAreaFeatureEntity>()
                .eq(GoodsAreaFeatureEntity::getGoodsId, dto.getGoodsId())
                .last("limit 1"));
        return obj;
    }

}
